﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBUtlity;
using Models;
using System.Data;
using System.Data.SqlClient;
namespace DAL
{
    public class PJLogService
    {
        #region 新增
        /// <summary>
        /// 新增一条记录
        /// </summary>
        /// <param name="baojia"></param>
        /// <returns></returns>
        public int InsertLog(PJLog pjLog)
        {
            string sql = "insert into PJLogs(WorkParentId,PJId,Stage,Node,Problem,Source,Person,Department,Statues1,Degree,StartDate,CompleteDate,RealCompleteDate,Ps)";
            sql += "values(@WorkParentId,@PJId,@Stage,@Node,@Problem,@Source,@Person,@Department,@Statues1,@Degree,@StartDate,@CompleteDate,@RealCompleteDate,@Ps)";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@WorkParentId",pjLog.WorkParentId),
                
                new SqlParameter("@PJId",pjLog.PJId),
                new SqlParameter("@Stage",pjLog.Stage),
                new SqlParameter("@Node",pjLog.Node),
                new SqlParameter("@Problem",pjLog.Problem),
                new SqlParameter("@Source",pjLog.Source),
                new SqlParameter("@Person",pjLog.Person),
                new SqlParameter("@Department",pjLog.Department),
                new SqlParameter("@Statues1",pjLog.Statues1),
                new SqlParameter("@Degree",pjLog.Degree),
                new SqlParameter("@StartDate",pjLog.StartDate),
                new SqlParameter("@CompleteDate",pjLog.CompleteDate),
                new SqlParameter("@RealCompleteDate",pjLog.RealCompleteDate),
                new SqlParameter("@Ps",pjLog.Ps)
            };
            return SQLHelper.Update(sql, param);
        }
        public int InsertLogTemp(PJLog pjLog)
        {
            string sql = "insert into PJLogsTemp(WorkParentId,PJLogId，PJId,Stage,Node,Problem,Source,Person,Department,Statues1,Degree,StartDate,CompleteDate,RealCompleteDate,Ps,Reason)";
            sql += "values(@WorkParentId,@PJLogId,@PJId,@Stage,@Node,@Problem,@Source,@Person,@Department,@Statues1,@Degree,@StartDate,@CompleteDate,@RealCompleteDate,@Ps,@Reason)";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@WorkParentId",pjLog.WorkParentId),
                new SqlParameter("@PJLogId",pjLog.PJLogId),
                new SqlParameter("@PJId",pjLog.PJId),
                new SqlParameter("@Stage",pjLog.Stage),
                new SqlParameter("@Node",pjLog.Node),
                new SqlParameter("@Problem",pjLog.Problem),
                new SqlParameter("@Source",pjLog.Source),
                new SqlParameter("@Person",pjLog.Person),
                new SqlParameter("@Department",pjLog.Department),
                new SqlParameter("@Statues1",pjLog.Statues1),
                new SqlParameter("@Degree",pjLog.Degree),
                new SqlParameter("@StartDate",pjLog.StartDate),
                new SqlParameter("@CompleteDate",pjLog.CompleteDate),
                new SqlParameter("@RealCompleteDate",pjLog.RealCompleteDate),
                new SqlParameter("@Ps",pjLog.Ps),
                new SqlParameter("@Reason",pjLog.Reason)
            };
            return SQLHelper.Update(sql, param);
        }
        #endregion

        #region 更改
        public int UpdateLog(PJLog pjLog)
        {
            string sql = "update PJLogs Set Stage=@Stage,Node=@Node,Problem=@Problem,Source=@Source,";
            sql += "Person=@Person,Department=@Department,Statues1=@Statues1,Degree=@Degree,StartDate=@StartDate,";
            sql += "CompleteDate=@CompleteDate,RealCompleteDate=@RealCompleteDate,Ps=@Ps where WorkParentId=@WorkParentId";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@WorkParentId",pjLog.WorkParentId),
                new SqlParameter("@Stage",pjLog.Stage),
                new SqlParameter("@Node",pjLog.Node),
                new SqlParameter("@Problem",pjLog.Problem),
                new SqlParameter("@Source",pjLog.Source),
                new SqlParameter("@Person",pjLog.Person),
                new SqlParameter("@Department",pjLog.Department),
                new SqlParameter("@Statues1",pjLog.Statues1),
                new SqlParameter("@Degree",pjLog.Degree),
                new SqlParameter("@StartDate",pjLog.StartDate),
                new SqlParameter("@CompleteDate",pjLog.CompleteDate),
                new SqlParameter("@RealCompleteDate",pjLog.RealCompleteDate),
                new SqlParameter("@Ps",pjLog.Ps)
            };
            return SQLHelper.Update(sql, param);
        }
        public int UpdateLogById(PJLog pjLog)
        {
            string sql = "update PJLogs Set Stage=@Stage,Node=@Node,Problem=@Problem,Source=@Source,";
            sql += "Person=@Person,Department=@Department,Statues1=@Statues1,Degree=@Degree,StartDate=@StartDate,";
            sql += "CompleteDate=@CompleteDate,RealCompleteDate=@RealCompleteDate,Ps=@Ps where Id=@Id";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@Id",pjLog.PJLogId),
                new SqlParameter("@Stage",pjLog.Stage),
                new SqlParameter("@Node",pjLog.Node),
                new SqlParameter("@Problem",pjLog.Problem),
                new SqlParameter("@Source",pjLog.Source),
                new SqlParameter("@Person",pjLog.Person),
                new SqlParameter("@Department",pjLog.Department),
                new SqlParameter("@Statues1",pjLog.Statues1),
                new SqlParameter("@Degree",pjLog.Degree),
                new SqlParameter("@StartDate",pjLog.StartDate),
                new SqlParameter("@CompleteDate",pjLog.CompleteDate),
                new SqlParameter("@RealCompleteDate",pjLog.RealCompleteDate),
                new SqlParameter("@Ps",pjLog.Ps)
            };
            return SQLHelper.Update(sql, param);
        }
        public int UpdateLogTemp(PJLog pjLog)
        {
            string sql = "update PJLogsTemp Set Stage=@Stage,Node=@Node,Problem=@Problem,Source=@Source,";
            sql += "Person=@Person,Department=@Department,Statues1=@Statues1,Degree=@Degree,StartDate=@StartDate,";
            sql += "CompleteDate=@CompleteDate,RealCompleteDate=@RealCompleteDate,Ps=@Ps,Reason=@Reason where WorkParentId=@WorkParentId";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@WorkParentId",pjLog.WorkParentId),
                new SqlParameter("@Stage",pjLog.Stage),
                new SqlParameter("@Node",pjLog.Node),
                new SqlParameter("@Problem",pjLog.Problem),
                new SqlParameter("@Source",pjLog.Source),
                new SqlParameter("@Person",pjLog.Person),
                new SqlParameter("@Department",pjLog.Department),
                new SqlParameter("@Statues1",pjLog.Statues1),
                new SqlParameter("@Degree",pjLog.Degree),
                new SqlParameter("@StartDate",pjLog.StartDate),
                new SqlParameter("@CompleteDate",pjLog.CompleteDate),
                new SqlParameter("@RealCompleteDate",pjLog.RealCompleteDate),
                new SqlParameter("@Ps",pjLog.Ps),
                new SqlParameter("@Reason",pjLog.Reason)
            };
            return SQLHelper.Update(sql, param);
        }
        public int UpdateStatues(int workParentId)
        {
            string sql = "update PJLogs Set Statues=@Statues where WorkParentId=@WorkParentId";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@WorkParentId",workParentId),
                new SqlParameter("@Statues","1")
            };
            return SQLHelper.Update(sql, param);
        }
        public int UpdateStatuesTemp(int workParentId)
        {
            string sql = "update PJLogsTemp Set Statues=@Statues where WorkParentId=@WorkParentId";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@WorkParentId",workParentId),
                new SqlParameter("@Statues","1")
            };
            return SQLHelper.Update(sql, param);
        }
        #endregion

        #region 查询
        public  PJLog GetLog(int workParentId)
        {
            string sql = "select * from PJLogs where WorkParentId=@WorkParentId";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@WorkParentId",workParentId)
            };
            SqlDataReader objReader = SQLHelper.GetReader(sql, param);
            PJLog result = null;
            if(objReader.Read())
            {
                result = new PJLog()
                {
                    WorkParentId = Convert.ToInt32(objReader["WorkParentId"]),
                    PJId = objReader["PJId"].ToString(),
                    Stage = objReader["Stage"].ToString(),
                    Node = objReader["Node"].ToString(),
                    Problem = objReader["Problem"].ToString(),
                    Source = objReader["Source"].ToString(),
                    Person = objReader["Person"].ToString(),
                    Department = objReader["Department"].ToString(),
                    Statues1 = objReader["Statues1"].ToString(),
                    Degree = objReader["Degree"].ToString(),
                    StartDate = objReader["StartDate"] is DBNull ? Convert.ToDateTime("1900-01-01") : Convert.ToDateTime(objReader["StartDate"]),
                    CompleteDate = objReader["CompleteDate"] is DBNull ? Convert.ToDateTime("1900-01-01") : Convert.ToDateTime(objReader["CompleteDate"]),
                    RealCompleteDate = objReader["RealCompleteDate"] is DBNull ? Convert.ToDateTime("1900-01-01") : Convert.ToDateTime(objReader["RealCompleteDate"]),
                    StrStartDate = objReader["StartDate"] is DBNull ? "" : Convert.ToDateTime(objReader["StartDate"]).ToString("yyyy-MM-dd"),
                    StrCompleteDate = objReader["CompleteDate"] is DBNull ? "" : Convert.ToDateTime(objReader["CompleteDate"]).ToString("yyyy-MM-dd"),
                    StrRealCompleteDate = objReader["RealCompleteDate"] is DBNull ? "" : Convert.ToDateTime(objReader["RealCompleteDate"]).ToString("yyyy-MM-dd"),
                    Ps = objReader["Ps"].ToString(),
                    Statues = objReader["Statues"].ToString() == "0" ? "未同步" : "已同步"
                };
            }
            objReader.Close();
            return result;
        }
        public PJLog GetLogFormId(int id)
        {
            string sql = "select * from PJLogs where Id=@Id";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@Id",id)
            };
            SqlDataReader objReader = SQLHelper.GetReader(sql, param);
            PJLog result = null;
            if (objReader.Read())
            {
                result = new PJLog()
                {
                    PJLogId = id,
                    WorkParentId = Convert.ToInt32(objReader["WorkParentId"]),
                    PJId = objReader["PJId"].ToString(),
                    Stage = objReader["Stage"].ToString(),
                    Node = objReader["Node"].ToString(),
                    Problem = objReader["Problem"].ToString(),
                    Source = objReader["Source"].ToString(),
                    Person = objReader["Person"].ToString(),
                    Department = objReader["Department"].ToString(),
                    Statues1 = objReader["Statues1"].ToString(),
                    Degree = objReader["Degree"].ToString(),
                    StartDate = objReader["StartDate"] is DBNull ? Convert.ToDateTime("1900-01-01") : Convert.ToDateTime(objReader["StartDate"]),
                    CompleteDate = objReader["CompleteDate"] is DBNull ? Convert.ToDateTime("1900-01-01") : Convert.ToDateTime(objReader["CompleteDate"]),
                    RealCompleteDate = objReader["RealCompleteDate"] is DBNull ? Convert.ToDateTime("1900-01-01") : Convert.ToDateTime(objReader["RealCompleteDate"]),
                    Ps = objReader["Ps"].ToString(),
                    Statues = objReader["Statues"].ToString() == "0" ? "未同步" : "已同步",
                    StrStartDate = objReader["StartDate"] is DBNull ? "" : Convert.ToDateTime(objReader["StartDate"]).ToString("yyyy-MM-dd"),
                    StrCompleteDate = objReader["CompleteDate"] is DBNull ? "" : Convert.ToDateTime(objReader["CompleteDate"]).ToString("yyyy-MM-dd"),
                    StrRealCompleteDate = objReader["RealCompleteDate"] is DBNull ? "" : Convert.ToDateTime(objReader["RealCompleteDate"]).ToString("yyyy-MM-dd")
                };
            }
            objReader.Close();
            return result;
        }
        public PJLog GetLogTemp(int workParentId)
        {
            string sql = "select * from PJLogsTemp where WorkParentId=@WorkParentId";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@WorkParentId",workParentId)
            };
            SqlDataReader objReader = SQLHelper.GetReader(sql, param);
            PJLog result = null;
            if (objReader.Read())
            {
                result = new PJLog()
                {
                    WorkParentId = Convert.ToInt32(objReader["WorkParentId"]),
                    PJLogId = Convert.ToInt32(objReader["PJLogId"]),
                    PJId = objReader["PJId"].ToString(),
                    Stage = objReader["Stage"].ToString(),
                    Node = objReader["Node"].ToString(),
                    Problem = objReader["Problem"].ToString(),
                    Source = objReader["Source"].ToString(),
                    Person = objReader["Person"].ToString(),
                    Department = objReader["Department"].ToString(),
                    Statues1 = objReader["Statues"].ToString(),
                    Degree = objReader["Degree"].ToString(),
                    StartDate = objReader["StartDate"] is DBNull ? Convert.ToDateTime("1900-01-01") : Convert.ToDateTime(objReader["StartDate"]),
                    CompleteDate = objReader["CompleteDate"] is DBNull ? Convert.ToDateTime("1900-01-01") : Convert.ToDateTime(objReader["CompleteDate"]),
                    RealCompleteDate = objReader["RealCompleteDate"] is DBNull ? Convert.ToDateTime("1900-01-01") : Convert.ToDateTime(objReader["RealCompleteDate"]),
                    StrStartDate = objReader["StartDate"] is DBNull ? "" : Convert.ToDateTime(objReader["StartDate"]).ToString("yyyy-MM-dd"),
                    StrCompleteDate = objReader["CompleteDate"] is DBNull ? "" : Convert.ToDateTime(objReader["CompleteDate"]).ToString("yyyy-MM-dd"),
                    StrRealCompleteDate = objReader["RealCompleteDate"] is DBNull ? "" : Convert.ToDateTime(objReader["RealCompleteDate"]).ToString("yyyy-MM-dd"),
                    Ps = objReader["Ps"].ToString(),
                    Reason= objReader["Reason"].ToString(),
                    Statues = objReader["Statues"].ToString() == "0" ? "未同步" : "已同步"
                    
                };
            }
            objReader.Close();
            return result;
        }
        public List<PJLog> GetLog(string pjId)
        {
            string sql = "select * from PJLogs where PJId=@PJId";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@PJId",pjId)
            };
            SqlDataReader objReader = SQLHelper.GetReader(sql, param);
            List<PJLog> result = new List<PJLog>();
            while (objReader.Read())
            {
                result.Add(new PJLog()
                {
                    Id = Convert.ToInt32(objReader["Id"]),
                    WorkParentId = Convert.ToInt32(objReader["WorkParentId"]),
                    PJId = objReader["PJId"].ToString(),
                    Stage = objReader["Stage"].ToString(),
                    Node = objReader["Node"].ToString(),
                    Problem = objReader["Problem"].ToString(),
                    Source = objReader["Source"].ToString(),
                    Person = objReader["Person"].ToString(),
                    Department = objReader["Department"].ToString(),
                    Statues1 = objReader["Statues"].ToString(),
                    Degree = objReader["Degree"].ToString(),
                    StartDate = objReader["StartDate"] is DBNull ? Convert.ToDateTime("1900-01-01") : Convert.ToDateTime(objReader["StartDate"]),
                    CompleteDate = objReader["CompleteDate"] is DBNull ? Convert.ToDateTime("1900-01-01") : Convert.ToDateTime(objReader["CompleteDate"]),
                    RealCompleteDate = objReader["RealCompleteDate"] is DBNull ? Convert.ToDateTime("1900-01-01") : Convert.ToDateTime(objReader["RealCompleteDate"]),
                    Ps = objReader["Ps"].ToString(),
                    //Reason = objReader["Reason"].ToString(),
                    Statues = objReader["Statues"].ToString() == "0" ? "未同步" : "已同步"
                });
                    

            }
            objReader.Close();
            return result;
        }
        #endregion
    }
}
